package com.nwl.crm.service;

import com.nwl.crm.dao.EmployeeMapper;
import com.nwl.crm.dto.EmployeeDto;
import com.nwl.crm.po.Employee;
import com.nwl.crm.po.EmployeeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class EmployeeService extends AbstractService<EmployeeDto>{

    @Autowired
    private EmployeeMapper mapper;

    @Override
    public void getAll(EmployeeDto dto) {

    }

    @Override
    public void layuiList(EmployeeDto dto) {
        EmployeeExample example = new EmployeeExample();

        if(!StringUtils.isEmpty(dto.getPo())){
            EmployeeExample.Criteria criteria = example.createCriteria();
            if(!StringUtils.isEmpty(dto.getPo().getTel())){
                criteria.andTelEqualTo(dto.getPo().getTel());
            }
            if(!StringUtils.isEmpty(dto.getPo().getUserName())){
                criteria.andUserNameLike("%"+dto.getPo().getUserName()+"%");
            }
            if(!StringUtils.isEmpty(dto.getPo().getRealName())){
                criteria.andRealNameLike("%"+dto.getPo().getRealName()+"%");
            }
        }


        //获取数据里面的总条数
        long count = mapper.countByExample(example);
        //获取当前页的数据
        List<Employee> data = mapper.selectByExample(example);

        dto.getPageUtil().setCount(count);
        dto.getPageUtil().setData(data);
    }

    @Override
    public void saveOrUpdate(EmployeeDto dto) {
        //在保存数据时存储id，需要随机生成
        dto.getPo().setId(UUID.randomUUID().toString());

        dto.getPo().setInputTime(new Date());

        //默认密码为000000
        dto.getPo().setPassword("000000");

        mapper.insert(dto.getPo());
    }

    @Override
    public void delete(EmployeeDto dto) {
        //根据逗号将多条数据的id拆分为id数据
        String[] ids = dto.getIds().split(",");
        for(int i=0;i<ids.length;i++){
            mapper.deleteByPrimaryKey(ids[i]);
        }

    }
}
